<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "photo_comment".
 *
 * @property int $id
 * @property int $uid 用户id
 * @property int $pid 图片id
 * @property string $comment 评论
 * @property string $craeted_at 创建时间
 * @property string $status
 *
 * @property Photo $p
 * @property User $u
 */
class PhotoComment extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'photo_comment';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['pid', 'comment'], 'required'],
            [['uid', 'pid'], 'integer'],
            [['comment', 'craeted_at', 'status'], 'string', 'max' => 255],
            [
                ['pid'],
                'exist',
                'skipOnError'     => true,
                'targetClass'     => Photo::className(),
                'targetAttribute' => ['pid' => 'id']
            ],
            [
                ['uid'],
                'exist',
                'skipOnError'     => true,
                'targetClass'     => User::className(),
                'targetAttribute' => ['uid' => 'id']
            ],
        ];
    }

    public function beforeSave($insert)
    {

        if (parent::beforeSave($insert)) {
            if ($insert) {
                $this->craeted_at =(string) date('Y-m-d H:i:s');
            }

            return true;
        } else {
            return false;
        }

    }


    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id'         => 'ID',
            'uid'        => '用户id',
            'pid'        => '图片id',
            'comment'    => '评论',
            'craeted_at' => '创建时间',
            'status'     => 'Status',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getP()
    {
        return $this->hasOne(Photo::className(), ['id' => 'pid']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getU()
    {
        return $this->hasOne(User::className(), ['id' => 'uid']);
    }
}
